//
//  NewHomeCollectionViewCell.m
//  quxiangtao
//
//  Created by E人一铺 on 2019/2/20.
//  Copyright © 2019年 shanhaijin. All rights reserved.
//

#import "NewHomeCollectionViewCell.h"
#import "NewHomeModel.h"
#import "BuyOrderContentDetailViewController.h"
#import "ShippingAddressAddViewController.h"

@interface NewHomeCollectionViewCell()

@property (nonatomic,strong) UIImageView *goodsImageView;
@property (nonatomic,strong) UILabel *goodsTitle;
@property (nonatomic,strong) UILabel *price;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UILabel *count;

@end

@implementation NewHomeCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.contentView.backgroundColor = WhiteColor;
        [self.contentView addSubview:self.goodsImageView];
        [self.contentView addSubview:self.goodsTitle];
        [self.contentView addSubview:self.price];
        [self.contentView addSubview:self.btn];
        [self.contentView addSubview:self.count];
        
        
    }
    return self;
}

- (void)setModel:(NewHomeModel *)model {
    
    _model = model;
    self.goodsTitle.text = model.goods_name;
    self.price.text = [NSString stringWithFormat:@"¥ %@", model.goods_price];
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.goods_logo]];
    
    self.count.text = [NSString stringWithFormat:@"已砍%@件", model.bargainCount];
    
}
- (void)btnClick {
    NSLog(@"ddddf");
    
    if (![[CommonAction sharedInstance] isLoginWithParentVc:self.parentVc]) {
        return;
    }
    

    
    [PPNetworkHelper POST:@"bargain/bargaingoods/defaultAdresApp" parameters:@{@"userid":[CommonUserInfo sharedInstance].user_id} success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            NSDictionary *dict = responseObject[@"data"];
            NSString *addressid = dict[@"addressid"];
            
            NSDictionary *contentDict = @{@"sid":self.model.ID,
                                          @"addressid":addressid,
                                          @"kdmoney":self.model.goods_kdmoney
                                          };

            if ([addressid intValue] != 0) {

                BuyOrderContentDetailViewController *vc = [[BuyOrderContentDetailViewController alloc] init];
                vc.tradeType = 0;
                vc.dict = contentDict;
                [self.parentVc.navigationController pushViewController:vc animated:YES];

            }else {

                ShippingAddressAddViewController *vc = [[ShippingAddressAddViewController alloc] init];
                vc.type = 1;
                vc.title = @"添加地址";
                vc.goodsInfoDict = [NSMutableDictionary dictionaryWithDictionary:contentDict];
                [self.parentVc.navigationController pushViewController:vc animated:YES];

            }

            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
            
            NSLog(@"error:%@", error);

        
    }];
    
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    
    [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.contentView.mas_centerX);
        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(jisuanH(12));
            make.height.mas_equalTo(jisuanH(330));
            make.width.mas_equalTo(jisuan(332));
        
    }];
    
    [self.goodsTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.goodsImageView);
        make.top.mas_equalTo(weakSelf.goodsImageView.mas_bottom).with.offset(jisuan(22));
    }];
    
    [self.price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.goodsImageView);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(-jisuanH(14));
    }];
    
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(jisuan(-10));
        make.centerY.mas_equalTo(weakSelf.price.mas_centerY);
        make.width.mas_equalTo(jisuan(150));
        make.height.mas_equalTo(jisuan(50));
    }];
    
    [self.count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.goodsImageView);
        make.top.mas_equalTo(weakSelf.goodsTitle.mas_bottom).with.offset(jisuan(16));
        make.bottom.mas_equalTo(weakSelf.price.mas_top).with.offset(-jisuan(10));
    }];
    
}
#pragma mark - 懒加载
- (UILabel *)count {
    if (_count == nil) {
        _count = [[UILabel alloc] init];
        _count.font = Font(20);
        _count.text = @"已售：221232 件";
        _count.numberOfLines = 2;
        _count.textColor = [UIColor colorWithHexString:@"333333"];
        
    }
    return _count;
}


- (UIButton *)btn {
    if (_btn == nil) {
        _btn = [[UIButton alloc] init];
        _btn.titleLabel.font = Font(24);
        [_btn setTitle:@"砍价免费拿" forState:UIControlStateNormal];
        
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn setBackgroundImage:[UIImage imageNamed:@"btn矩形"] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _btn;
}

- (UILabel *)price {
    if (_price == nil) {
        _price = [[UILabel alloc] init];
        _price.font = Font(30);
        _price.text = @"¥109.00";
        _price.textColor = [UIColor colorWithHexString:@"333333"];

    }
    return _price;
}

- (UILabel *)goodsTitle {
    if (_goodsTitle == nil) {
        _goodsTitle = [[UILabel alloc] init];
        _goodsTitle.font = Font(24);
        _goodsTitle.text = @"";
        _goodsTitle.numberOfLines = 2;
        _goodsTitle.textColor = [UIColor colorWithHexString:@"333333"];
        
    }
    return _goodsTitle;
}

- (UIImageView *)goodsImageView {
    if (_goodsImageView == nil) {
        _goodsImageView = [[UIImageView alloc] init];
        _goodsImageView.image = [UIImage imageNamed:@"Yosemite01"];
    }
    return _goodsImageView;
}

@end
